From: Wei Liu Date: Wed, 7 Jan 2015 15:23:00 +0000 (+0000) Subject: libxl_internal: comment on domain userdata unlock function X-Git-Tag: archive/raspbian/4.8.0-1+rpi1~1^2~3965 X-Git-Url: https://dgit.raspbian.org/%22http://www.example.com/cgi/%22/%22http:/www.example.com/cgi/%22?a=commitdiff_plain;h=ad40b1fbe41b2fc3a0dfddbbe07a9678c8cd1921;p=xen.git libxl_internal: comment on domain userdata unlock function Discuss why we need to unlink file path before closes fd. Signed-off-by: Wei Liu Cc: Ian Campbell Cc: Ian Jackson Acked-by: Ian Jackson [ ijc -- s/to avoid such/to avoid the following/ as requested by Ian ] --- diff --git a/tools/libxl/libxl_internal.c b/tools/libxl/libxl_internal.c index 9d8025de48..ddc68ab2bd 100644 --- a/tools/libxl/libxl_internal.c +++ b/tools/libxl/libxl_internal.c @@ -458,6 +458,20 @@ out: void libxl__unlock_domain_userdata(libxl__domain_userdata_lock *lock) { + /* It's important to unlink the file before closing fd to avoid + * the following race (if close before unlink): + * + * P1 LOCK P2 UNLOCK + * fd1 = open(lockfile) + * close(fd2) + * flock(fd1) + * fstat and stat check success + * unlink(lockfile) + * return lock + * + * In above case P1 thinks it has got hold of the lock but + * actually lock is released by P2 (lockfile unlinked). + */ if (lock->path) unlink(lock->path); if (lock->carefd) libxl__carefd_close(lock->carefd); free(lock->path);